package CarPackage;

import Util.DBUtil;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class CarDao {
    public int addToCar(String goodsId, String goodsName, String unitPrice, String userId, String pic) throws Exception {
        if (checkIfItemExists(goodsId, userId)) {
            // 更新数量
            String updateSql = "UPDATE car SET quantity = quantity + 1 WHERE goods_id = ? AND userid = ?";
            try (Connection connection = DBUtil.getConnection();
                 PreparedStatement stmt = connection.prepareStatement(updateSql)) {
                stmt.setString(1, goodsId);
                stmt.setString(2, userId);
                return stmt.executeUpdate();
            }
        } else {
            // 添加新物品
            String insertSql = "INSERT INTO car (goods_id, goods_name, unitprice, quantity, userid, pic) VALUES (?, ?, ?, ?, ?, ?)";
            try (Connection connection = DBUtil.getConnection();
                 PreparedStatement stmt = connection.prepareStatement(insertSql)) {
                stmt.setString(1, goodsId);
                stmt.setString(2, goodsName);
                stmt.setString(3, unitPrice);
                stmt.setString(4, "1");
                stmt.setString(5, userId);
                stmt.setString(6, pic);
                return stmt.executeUpdate();
            }
        }
    }

    public int removeFromCar(String goodsId, String userId) throws Exception {
        String sql = "DELETE FROM car WHERE goods_id = ? AND userid = ?";
        try (Connection connection = DBUtil.getConnection();
             PreparedStatement stmt = connection.prepareStatement(sql)) {

            stmt.setString(1, goodsId);
            stmt.setString(2, userId);
            return stmt.executeUpdate();
        }
    }

    public List<Car> getCarByUserId(String userId) throws Exception {
        String sql = "SELECT * FROM car WHERE userid=?";
        try (Connection connection = DBUtil.getConnection();
             PreparedStatement stmt = connection.prepareStatement(sql)) {

            stmt.setString(1, userId);
            ResultSet rs = stmt.executeQuery();
            List<Car> carList = new ArrayList<>();
            while (rs.next()) {
                Car car = new Car();
                car.setGoods_id(rs.getString("goods_id"));
                car.setGoods_name(rs.getString("goods_name"));
                car.setUnitprice(rs.getString("unitprice"));
                car.setQuantity(rs.getString("quantity"));
                car.setPic(rs.getString("pic"));
                carList.add(car);
            }
            return carList;
        }
    }
    public boolean checkIfItemExists(String goodsId, String userId) throws Exception {
        String sql = "SELECT COUNT(*) FROM car WHERE goods_id = ? AND userid = ?";
        try (Connection connection = DBUtil.getConnection();
             PreparedStatement stmt = connection.prepareStatement(sql)) {

            stmt.setString(1, goodsId);
            stmt.setString(2, userId);
            try (ResultSet rs = stmt.executeQuery()) {
                if (rs.next() && rs.getInt(1) > 0) {
                    return true; // 物品已存在
                } else {
                    return false; // 物品不存在
                }
            }
        }
    }

    public int updateQuantity(String goodsId, String userId, int change) throws Exception {
        System.out.println("更新数量 - Goods ID: " + goodsId + ", User ID: " + userId + ", Change: " + change);

        String sql = "UPDATE car SET quantity = quantity + ? WHERE goods_id = ? AND userid = ?";

        try (Connection connection = DBUtil.getConnection();
             PreparedStatement stmt = connection.prepareStatement(sql)) {

            stmt.setInt(1, change);
            stmt.setString(2, goodsId);
            stmt.setString(3, userId);

            int affectedRows = stmt.executeUpdate();
            System.out.println("受影响的行数: " + affectedRows);
            return affectedRows;
        }
    }

}
